package cn.tools;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Pattern;

import org.apache.commons.lang.time.DateFormatUtils;

public class DateHelper extends DateFormatUtils {

	private String minDate;
	private String maxDate;

	public String getMaxDate() {
		return maxDate;
	}

	public String getMinDate() {
		return minDate;
	}

	public static String formatDateToStr(int date) {
		SimpleDateFormat simpleDateFormat4Time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return simpleDateFormat4Time.format(date * 1000);
	}

	public void setDateType(int type) {
		Date date = new Date();
		Date today = new Date(System.currentTimeMillis());
		String mixDate = "";
		String maxDate = "";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
		maxDate = simpleDateFormat.format(today).toString();
		if (type == 1) {
			// 3天内
			date = new Date(System.currentTimeMillis() - (0x5265c00L * (3 - 1)));
			mixDate = simpleDateFormat.format(date).toString();
		} else if (type == 2) {
			// 一周内
			date = new Date(System.currentTimeMillis() - (0x5265c00L * (7 - 1)));
			mixDate = simpleDateFormat.format(date).toString();
		} else if (type == 3) {
			// 一月内
			date = new Date(System.currentTimeMillis() - (0x5265c00L * (30 - 1)));
			mixDate = simpleDateFormat.format(date).toString();
		} else if (type == 4) {
			// 三月内
			date = new Date(System.currentTimeMillis() - (0x5265c00L * (90 - 1)));
			mixDate = simpleDateFormat.format(date).toString();
		}
		this.maxDate = maxDate;
		this.minDate = mixDate;
		// return simpleDateFormat.format(date);
	}

	public void setDateType(int type, String datetype) {
		Date date = new Date();
		Date today = new Date(System.currentTimeMillis());
		String mixDate = "";
		String maxDate = "";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datetype);
		maxDate = simpleDateFormat.format(today).toString();
		if (type == 1) {
			// 3天内
			date = new Date(System.currentTimeMillis() - (0x5265c00L * (3 - 1)));
			mixDate = simpleDateFormat.format(date).toString();
		} else if (type == 2) {
			// 一周内
			date = new Date(System.currentTimeMillis() - (0x5265c00L * (7 - 1)));
			mixDate = simpleDateFormat.format(date).toString();
		} else if (type == 3) {
			// 一月内
			date = new Date(System.currentTimeMillis() - (0x5265c00L * (30 - 1)));
			mixDate = simpleDateFormat.format(date).toString();
		} else if (type == 4) {
			// 三月内
			date = new Date(System.currentTimeMillis() - (0x5265c00L * (90 - 1)));
			mixDate = simpleDateFormat.format(date).toString();
		} else if (type == 0) {
			date = new Date(System.currentTimeMillis() - (0x5265c00L * (0 - 1)));
			maxDate = simpleDateFormat.format(date).toString();
			mixDate = simpleDateFormat.format(today).toString();
		}
		this.maxDate = maxDate;
		this.minDate = mixDate;
	}

	/**
	 * 改变日志格式
	 * 
	 * @param datetime
	 * @param oldFormat
	 * @param newFormat
	 * @return
	 */
	public static String formatDateString(String datetime, String oldFormat, String newFormat) {
		Date date = null;
		try {
			if (datetime == null || datetime.equals("")) {
				return "";
			}
			SimpleDateFormat formatter = new SimpleDateFormat(oldFormat);
			date = formatter.parse(datetime);

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return getSpecifiedDate(date.getTime(), newFormat);
	}

	/**
	 * 根据指定的格式显示时间
	 * 
	 * @param time
	 *            long
	 * @param format
	 *            String author xieyan
	 * @return String
	 */
	public static String getSpecifiedDate(long time, String format) {
		if (time == 0) {
			return "-";
		}

		Date timeVal = new Date(time);

		String strTime = null;
		try {
			SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
			strTime = simpledateformat.format(timeVal);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return strTime;
	}

	/**
	 * 格式 ： 输出指定格式类型的时间
	 * 
	 * @param formater
	 *            String author xieyan
	 * @return String
	 */
	public static String getNowDate(String formater) {
		String strTime = null;
		try {
			SimpleDateFormat simpledateformat = new SimpleDateFormat(formater);
			strTime = simpledateformat.format(new Date());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return strTime;
	}

	public static void main(String args[]) {
		/*
		 * DateHelper dh = new DateHelper(); dh.setDateType(0,"yyyy-MM-dd"); System.out.println(dh.maxDate); System.out.println(dh.minDate);
		 */
		DateHelper.formatStringToLong("2010-10-07 12:31:00", DateHelper.FMT_DATE_DATETIME3);
		System.out.println(DateHelper.getDayEndSecond("2010-10-07"));
		System.out.println("------" + ("  aa  ".trim()) + "------");
	}

	/**
	 * string转换成long时间
	 * 
	 * @param dateString
	 * @param format
	 * @return
	 */
	public static long formatStringToLong(String dateString, String format) {
		long result = 0l;
		SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
		try {
			result = simpledateformat.parse(dateString).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String addDate(String strDate, int days) throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date cDate = df.parse(strDate);
		GregorianCalendar gcalendar = new GregorianCalendar();
		gcalendar.setTime(cDate);
		gcalendar.add(Calendar.DATE, days);
		return df.format(gcalendar.getTime()).toString();
	}

	public static String getDayEndSecond(String dateTime) {
		// String result = "";
		if (!dateTime.equals("")) {
			try {
				dateTime = DateHelper.formatDateString(dateTime + " 23:59:59", DateHelper.FMT_DATE_DATETIME, FMT_DATE_DATETIME);
			} catch (Exception e) {
			}
		}

		return dateTime;
	}

	public static Timestamp getTimestampNowDate() {
		Timestamp timestamp = null;
		try {
			timestamp = new Timestamp(new Date().getTime());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return timestamp;
	}

	public static final String FMT_DATE_YYYY = "yyyy";
	public static final String FMT_DATE_YYMMDD = "yyMMdd";
	public static final String FMT_DATE_YYYYMMDD = "yyyyMMdd";
	public static final String FMT_DATE_YYYY_MM_DD = "yyyy-MM-dd";
	public static final String FMT_DATE_YYYY_MM_DD2 = "yyyy/MM/dd";
	public static final String FMT_DATE_DATETIME = "yyyy-MM-dd HH:mm:ss";
	public static final String FMT_DATE_DATETIME3 = "yyyy-MM-dd HH:mm:sss";
	public static final String FMT_DATE_DATETIME2 = "yyyy-MM-dd HH:mm";
	public static final String FMT_DATE_DATETIME_TIGHT = "yyyyMMddHHmmss";
	public static final String FMT_DATE_YY_MM = "yy-MM";
	public static final String FMT_DATE_MM = "MM";
	public static final String FMT_DATE_DD = "dd";
	public static final String FMT_DATE_TIME = "HH:mm:ss";
	public static final Pattern DATE_PATTERN = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))");
}
